unit uMsgEventImpl;

interface

uses
  uPacket,
  uMsgClient,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,JyMessage_TLB,ActiveX, ComObj, StdCtrls;

type
  TMsgEventSink = class(TInterfacedObject, IUnknown,IDispatch)
  private
    {IUknown methods}
    function QueryInterface(const IID: TGUID; out Obj):HResult;stdcall;
    {Idispatch}
    function GetTypeInfoCount(out Count: Integer): HResult; stdcall;
    function GetTypeInfo(Index, LocaleID: Integer; out TypeInfo): HResult; stdcall;
    function GetIDsOfNames(const IID: TGUID; Names: Pointer;
      NameCount, LocaleID: Integer; DispIDs: Pointer): HResult; stdcall;
    function Invoke(DispID: Integer; const IID: TGUID; LocaleID: Integer;
      Flags: Word; var Params; VarResult, ExcepInfo, ArgErr: Pointer): HResult; stdcall;
  end;


implementation

{ TMsgEventSink }


function TMsgEventSink.GetIDsOfNames(const IID: TGUID; Names: Pointer;
  NameCount, LocaleID: Integer; DispIDs: Pointer): HResult;
begin
  Result := S_OK
end;

function TMsgEventSink.GetTypeInfo(Index, LocaleID: Integer;
  out TypeInfo): HResult;
begin
  Result := S_OK
end;

function TMsgEventSink.GetTypeInfoCount(out Count: Integer): HResult;
begin
  Result := S_OK
end;

function TMsgEventSink.Invoke(DispID: Integer; const IID: TGUID;
  LocaleID: Integer; Flags: Word; var Params; VarResult, ExcepInfo,
  ArgErr: Pointer): HResult;
var
  Args: PVariantArgList;
  iCode:Integer;
  sMsg:string;
  sError:String;
  lsHead,lsBody:TStringList;

begin
  Result := S_OK;
  Args := TDispParams(Params).rgvarg;
  sMsg:= OleVariant(Args^[0]);
  //parse the message
  if Not ParsePacket(sMsg,lsHead, lsBody,sError) then
  begin
    Exit;
  end;
  //get the handle

  case DispID of
    1: gMsgClient.DoJyMsg(sMsg);
  end;
end;

function TMsgEventSink.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterFace(IID,Obj) then
    Result := S_OK
  else if IsEqualIID(IID,IMsgClientEvents) then
    Result := QueryInterface(IDispatch,Obj)
  else
    Result := E_NOINTERFACE;
end;

end.
